package game.leon.war.node.handler;

import game.leon.engine.pattern.Fun;
import game.leon.war.defs.MessageConsts;
import game.leon.war.defs.Vo;
import game.leon.war.node.handler.letter.NodeLetter;
import game.leon.war.node.meta.Battle;
import game.leon.war.node.meta.Fighter;
import game.leon.war.node.service.BattleService;
import game.leon.war.node.service.MessageDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 创建一个战斗实例
 * User: bianpeng
 * Date: 12-1-31
 */
@Component
class CreateBattleHandler implements Handler<Vo.IdVO, Vo.BattleVO> {
    @Autowired
    private BattleService battleService;
    @Autowired
    private MessageDispatcher dispatcher;
    @Override
    public Vo.BattleVO exec(long userId, Vo.IdVO idVO) {
        Battle battle = battleService.createBattle(userId, idVO.getId());
        dispatcher.dispatch(new NodeLetter(userId, MessageConsts.BATTLE_TICK, System.currentTimeMillis()+3000));
        Vo.BattleVO.Builder builder = Vo.BattleVO.newBuilder();
        for(Fighter fighter : battle.getFighters()) {
            Vo.FightUnitVO.Builder vo = Vo.FightUnitVO.newBuilder();
            vo.setId(fighter.getId());
            vo.setSeat(fighter.getSeat());
            vo.setName(fighter.getName());
            vo.setHp(fighter.getHp());
            builder.addUnits(vo.build());
        }
        return builder.build();
    }
}
